package com.xuecheng.manage_media.util.foldermapping;

import com.xuecheng.framework.domain.media.folder.FolderMapping;
import com.xuecheng.framework.domain.media.folder.FolderMappingNode;
import com.xuecheng.framework.domain.media.request.FolderMappingRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

@Component
public class FolderMappingUtil {
    @Value("${xc-service-manage-media.video-location}")
    private  String upload_location;
    /**
     * 映射某个目录
     *
     * @return 结果
     */
    public  FolderMappingNode mapping() {
        File file = new File(upload_location);
        return Traverse(file);
    }

    public  FolderMappingNode Traverse(File files) {
        FolderMappingNode folderMappingNode = new FolderMappingNode();
        File[] listFiles = files.listFiles(new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                return !pathname.isFile();
            }
        });
        assert listFiles != null;
        List<FolderMapping> list = new ArrayList<>();
        for (File file : listFiles) {
            // 创建list 存入子节点
            FolderMappingNode folderMappingNode1 = new FolderMappingNode();
            // 会显示父节点
            folderMappingNode1.setId(file.getName());
            folderMappingNode1.setLabel(file.getName());
            FolderMappingNode traverse = Traverse(file);
            folderMappingNode1.setChildren(traverse.getChildren());
            list.add(folderMappingNode1);
        }
        folderMappingNode.setChildren(list);
        return folderMappingNode;
    }

    /**
     * 添加一个文件夹
     *
     * @param folderMappingRequest 文件夹的路径参数
     * @return 是否添加成功
     */
    public  boolean addFolder(FolderMappingRequest folderMappingRequest) {
        // 判断数据合法性
        if (folderMappingRequest == null
                || StringUtils.isEmpty(folderMappingRequest.getFolderName())
                || folderMappingRequest.getFolderPath().contains("\\")
                || folderMappingRequest.getFolderPath().contains("#")
                || folderMappingRequest.getFolderName().contains("/")
                || folderMappingRequest.getFolderName().contains("\\")
                || folderMappingRequest.getFolderName().contains("#")) {
            return false;
        }

        String folderPath = folderMappingRequest.getFolderPath();
        String folderName = folderMappingRequest.getFolderName();
        //判断是不是创建根目录创建根目录
        if (StringUtils.isEmpty(folderPath)) {
            File file = new File(upload_location +"/"+folderName);
            return file.mkdir();
        }else {
            // 创建目录
            System.out.println("路径" + folderPath);
            System.out.println("文件名" + folderName);
            File file = new File(upload_location +"/"+folderPath+"/"+folderName);
            System.out.println(file);
            return file.mkdir();
        }
    }

    /**
     * 删除一个文件夹
     *
     * @param folderMappingRequest 文件夹的路径参数
     * @return 是否删除成功
     */
    public  boolean delFolder(FolderMappingRequest folderMappingRequest) {
        // 判断数据合法性
        if (folderMappingRequest == null
                || StringUtils.isEmpty(folderMappingRequest.getFolderName())
                || folderMappingRequest.getFolderPath().contains("#")
                || folderMappingRequest.getFolderName().contains("/")
                || folderMappingRequest.getFolderName().contains("\\")
                || folderMappingRequest.getFolderName().contains("#")) {
            return false;
        }

        String folderPath = folderMappingRequest.getFolderPath();
        String folderName = folderMappingRequest.getFolderName();
        //判断是不是创建根目录创建根目录
        if (StringUtils.isEmpty(folderPath)) {
            System.out.println("路径" + folderPath);
            System.out.println("文件名" + folderName);
            File file = new File(upload_location +"/"+folderName);
            System.out.println(file);
            return file.delete();
        }else {
            // 创建目录
            System.out.println("路径" + folderPath);
            System.out.println("文件名" + folderName);
            File file = new File(upload_location +"/"+folderPath);
            System.out.println(file);
            return file.delete();
        }
    }
}
